﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Contracts.Security;

namespace XcoAppSpaces.Security.Basic
{
	/// <summary>
	/// Configuration string interpreter for the XcoBasicSecurityService.
	/// </summary>
	public class ConfigStringInterpreter : XcoConfigStringInterpreterBase
	{
		/// <summary>
		/// Config key for setting the policy.
		/// </summary>
		public const string ConfigPolicy = "security.policy";
		/// <summary>
		/// Config key for setting the path of the user store.
		/// </summary>
		public const string ConfigStore = "security.store";

		private XcoSecurityPolicy? policy;
		private string store;

		/// <summary>
		/// Action definition for interpretion
		/// </summary>
		public override Dictionary<string, Action<string>> InterpretDefinition
		{
			get
			{
				return new Dictionary<string, Action<string>> 
                    { 
                        {ConfigPolicy, AnalysePolicy},
                        {ConfigStore, AnalyseStore},
                    };
			}
		}

		/// <summary>
		/// Get the current service
		/// </summary>
		public override IXcoService GetService
		{
			get
			{
				if (policy != null || store != null)
				{
					var service = new XcoBasicSecurityService();
					if (store != null)
						service.StorePath = store;
					if (policy != null)
						service.Policy = policy.Value;
					return service;
				}
				return null;
			}
		}

		#region analyse methods

		void AnalysePolicy(string policyStr)
		{
			try
			{
				policy = (XcoSecurityPolicy)Enum.Parse(typeof(XcoSecurityPolicy), policyStr, true);
			}
			catch (Exception)
			{
				throw new XcoConfigurationException("Value \"" + policyStr + "\" is not a valid XcoSecurityPolicy!");
			}
		}

		void AnalyseStore(string storePath)
		{
			store = storePath;
		}
		#endregion

	}
}
